// components/cash-withdrawal/cash-withdrawal.js
import Toast from '../../miniprogram/miniprogram_npm/vant-weapp/toast/toast'
import Dialog from '../../miniprogram/miniprogram_npm/vant-weapp/dialog/dialog'
let app = getApp();

Component({

  options: {
    addGlobalClass: true,//使用app.wxss样式
  },
  /**
   * 组件的属性列表
   */
  properties: {
    isPickMoneyBranch: {
      type: Boolean,
      observer(newVal,oldVal,changedPath){
        this.setData({
          isPickMoney: newVal
        })
      }
    },
    userBranch: {
      type: Object,
      observer(newVal,oldVal,changedPath){
        this.setData({
          user: newVal
        })
      }
    },
    balanceBranch: {
      type: Number,
      observer(newVal,oldVal,changedPath){
        this.setData({
          balance: newVal
        })
      }
    },
    isG: Number,// 1供应商
  },

  /**
   * 组件的初始数据
   */
  data: {
    user: '',
    isPickMoney: false,//控制提现弹框
    isBank: false,//控制银行卡
    nowBank: '',//选择的银行卡数据
    bankList: [],//银行卡接口数据
    bankColumns: [],//银行卡弹框数据
    balance: '',//可提现余额，账户余额或供应商余额
    money: '',//提现金额
    cashWay:'银行卡',
  },

  lifetimes: {
    ready () {
//      this.getBankList();
    },
  },

  /**
   * 组件的方法列表
   */
  methods: {
    //全部提现
    pickAll(){
      let _this = this;
      _this.setData({
        money: this.data.balance
      })
    },
    // 选择提现方式，是微信钱包还是银行卡
    selectCashWay:function(){
      let that = this;
      wx.showActionSheet({
        itemList: ['微信钱包', '银行卡'],
        success: function(res) {
          console.log(res.tapIndex);
          if(res.tapIndex == 0){
            that.setData({
              cashWay:'微信钱包'
            })
          }else{
            that.setData({
              cashWay:'银行卡'
            })
          }
        },
        fail: function(res) {
          console.log(res.errMsg)
        }
      })
    },
    //申请提请
    pickMoney(){
      let _this = this;
      if(_this.data.money == ''){
        wx.showToast({
          title: '请填写提现金额',
          icon: 'none'
        });
        return;
      } else if(parseFloat(_this.data.money) < 1){
        wx.showToast({
          title: '提现金额不可小于1￥',
          icon: 'none'
        });
        return;
      } else if(_this.data.money > _this.data.balance){
        wx.showToast({
          title: '余额不足',
          icon: 'none'
        });
        return;
      }
      if(_this.data.cashWay == '银行卡'){
        if(!_this.data.nowBank){
          wx.navigateTo({
            url: '/sub/user/bank-card-add/bank-card-add?cardData=undefined'
          })
          return;
        }
      }

      _this.pickGoGo();
    },

    pickGoGo(){
      let _this = this;
      _this.setData({
        isPickMoney: false
      });
      let cardNo = _this.data.nowBank.cardNo;
      if(cardNo == undefined || _this.data.cashWay == '微信钱包'){
        cardNo = '';
      };
      this.triggerEvent('isPickMoney', this.data.isPickMoney);
      Dialog.confirm({
        title: '提现',
        message: '是否确认提现？'
      }).then(()=>{
        app.requestUrl({
          method: 'POST',
          url: '/wxapi/balance/pick',
          data: {
            memberId: _this.data.user.id,
            money: _this.data.money,
            isgys: _this.properties.isG,
            bankcard: cardNo
          },
        }).then(res =>{
          Toast('提现申请成功');
          _this.setData({
            'user.credit2': _this.properties.isG == 1 ? _this.data.user.credit2 : _this.data.user.credit2 - _this.data.money
        })
          _this.triggerEvent('user', _this.data.user);
        }).catch(() => {
          Toast.fail("提现申请失败");
        });
      })
    },

    //选择银行卡确定
    bankConfirm(event){
      let _this = this;
      _this.setData({
        nowBank: _this.data.bankList[event.detail.index]
      })
      _this.isBankMonitor();
    },

    //银行卡数据
    getBankList(){
      let _this = this;
      app.requestUrl({
        method: 'GET',
        url: '/wxapi/bank/listcard',
        data: {
          memberId: _this.data.user.id,
          limit: 0,
          offset: 0
        },
      }).then(res =>{
        let rows = res.data.data.rows;
        _this.data.bankColumns = [];
        rows.forEach(item =>{
          _this.data.bankColumns.push(item.bbName +'('+ item.cardNo.substring(item.cardNo.length-4) +')')
        })
        _this.setData({
          bankColumns: _this.data.bankColumns,
          bankList: rows,
          nowBank: rows[0] || ''
        })
      })
    },

    //提现弹框，阻止关闭
    pickClose(event){
      let _this = this;
      if(event.detail == 'confirm'){
        _this.pickMoney();
        _this.setData({
          isPickMoney: true
        })
        this.triggerEvent('isPickMoney', this.data.isPickMoney);
      } else {
        this.triggerEvent('isPickMoney', false);
      }
    },
    //提现弹框
    pickMonitor(){
      let _this = this;
      this.setData({
        isPickMoney: true
      })
      this.triggerEvent('isPickMoney', this.data.isPickMoney);
    },
    //银行卡弹框
    isBankMonitor(){
      let _this = this;
      if(!_this.data.nowBank){
        wx.navigateTo({
          url: '/sub/user/bank-card-add/bank-card-add?cardData=undefined'
        })
        return;
      }
      this.setData({
        isBank: !_this.data.isBank
      })
    },
    moneyBlur(event){
      this.setData({money: event.detail.value})
    },
    toPickMoneyList:function(){
      wx.navigateTo({
        url: '/sub/user/pick-money-list/pick-money-list'
      })
    },
  }
})
